package com.qianxun.module.base.constant;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 分页信息
 *
 * @author: huwei
 * @date: 2019/10/25 9:24
 * @version: 1.0.0
 */
@Data
@Accessors(chain = true)
public class PageInfo {
    /**
     * 默认当前页
     */
    private static final int DEFAULT_CURRENT = 1;
    /**
     * 默认显示数
     */
    private static final int DEFAULT_SIZE = 20;
    /**
     * 当前页
     */
    private int current;
    /**
     * 显示数量
     */
    private int size;

    /**
     * 选择正确的分页数
     *
     * @param current 当前页
     * @param size    显示数
     * @return 分页信息
     */
    public static PageInfo build(Integer current, Integer size) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setCurrent(current == null || current < 0 ? DEFAULT_CURRENT : current);
        pageInfo.setSize(size == null || size < 0 ? DEFAULT_SIZE : size);
        return pageInfo;
    }

    public int startIndex() {
        return (current - 1) * size;
    }

    public int endIndex() {
        return size;
    }

    public int getCurrent() {
        return current <= 0 ? DEFAULT_CURRENT : current;
    }

    public int getSize() {
        return size <= 0 ? DEFAULT_SIZE : size;
    }
}
